package com.taskScheduler.pool;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;

import com.taskScheduler.task.InstantTaskInfo;
import com.taskScheduler.task.PoolTask;

public class InstantTaskPool {
	/**
	 * ThreadPoolExecutors to manage the transfer of the task
	 */
	private ThreadPoolExecutor executor;
	public InstantTaskPool(int poolSize){
		executor=(ThreadPoolExecutor)Executors.newFixedThreadPool(poolSize);
	}
	/*Now we will use ConcurrentLinkedQueue, in the future we may use Queue and do the synchronied work ourselves.*/
	//(taskInfoQueue,runnableTaskQueue)
	public void excuteTasks(ConcurrentLinkedQueue<InstantTaskInfo> queue,BlockingQueue<Runnable> runnableTaskqueue){
		while(!queue.isEmpty()){
			InstantTaskInfo task =queue.poll();
			PoolTask poolTask=new PoolTask(task,runnableTaskqueue);
			executor.execute(poolTask);
		}
	}
	
	/**
	 * This method shuts down the executor
	 */
	public void endServer() {
		executor.shutdown();
	}
}
